import sys
import time
import sys

from PyQt5 import QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import requests  # 导入requests包
import re  # 导入re包，正则表达式使用
ARK = {
    170 : [73043,0],
    576 : [3753870,0],
    312 : [1756005,0],
    618 : [5753765,0],
}
def server_people(server):
    try:
        url = f"https://www.battlemetrics.com/servers/ark/{ARK[server][0]}"  # 需要请求的网址
        html_str = requests.get(url)  # 发送Get请求
        # html_str 就是 html里面的所有内容
        people = re.findall(r"<dt>Player count</dt><dd>(.+?)</dd>",
                            html_str.text)  # 通过正则表达式 r"<a.*?>.*?</a>" 找到所有的数据并输出
        server_name = 'server_name'
        if ARK[server][0] == 73043:
            server_name = 'EU-PVP-Official-TheIsland170 '
        elif ARK[server][0] == 1756005:
            server_name = "NA-PVP-Official-Aberration312"
        elif ARK[server][0] == 3753870:
            server_name = 'NA-PVP-Official-Valguero576  '
        elif ARK[server][0] == 5753765:
            server_name = 'NA-PVP-Official-GenOne618    '
        print(server_name,people[0])
        return people[0]
        # # 123
        # ARK[server][1] = people[0]
    except:
        pass




class WorkThread(QThread):
    # 初始化线程
    def __int__(self):
        super(WorkThread, self).__init__()
    #线程运行函数
    def run(self):
        while True:
            global T_test
            datetime = QDateTime.currentDateTime()
            T_test = datetime.toString()

class WorkThread1(QThread):
    # 初始化线程
    def __int__(self):
        super(WorkThread1, self).__init__()
    #线程运行函数
    def run(self):
        while True:
            global Server_170
            Server_170 = server_people(170)

            global Server_576
            Server_576 = server_people(576)

            global Server_618
            Server_618 = server_people(618)

            global Server_312
            Server_312 = server_people(312)


class showTime(QDialog):
    def __init__(self):
        super(showTime, self).__init__()
        self.move(0,0)
        self.setWindowFlags(Qt.FramelessWindowHint | Qt.Tool)# | Qt.WindowStaysOnTopHint
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.setWindowTitle("LF_NO.1")
        self.label = QLabel(self)
        self.label.setFixedWidth(250)
        self.label.move(20,0)
        self.label.setStyleSheet("QLabel{color: red;opacity:1;font-size:20px;font-weight:bold;font-family:宋体;}")

        self.label1 = QLabel(self)
        self.label1.setFixedWidth(350)
        self.label1.move(20, 20)
        self.label1.setStyleSheet("QLabel{color: red;font-size:20px;font-weight:bold;font-family:宋体;}")

        #1111111
        #1111111
        self.label1_1 = QLabel(self)
        self.label1_1.setFixedWidth(100)#右边的长度(加上人数宽度了的)
        self.label1_1.move(350, 20)
        self.label1_1.setStyleSheet("QLabel{color: red;font-size:20px;font-weight:bold;font-family:宋体;}")

        self.label2 = QLabel(self)
        self.label2.setFixedWidth(350)
        self.label2.move(20, 40)
        self.label2.setStyleSheet("QLabel{color: red;font-size:20px;font-weight:bold;font-family:宋体;}")

        self.label2_1 = QLabel(self)
        self.label2_1.setFixedWidth(100)#右边的长度(加上人数宽度了的)
        self.label2_1.move(350, 40)
        self.label2_1.setStyleSheet("QLabel{color: red;font-size:20px;font-weight:bold;font-family:宋体;}")
        #
        self.label3 = QLabel(self)
        self.label3.setFixedWidth(350)
        self.label3.move(20, 60)
        self.label3.setStyleSheet("QLabel{color: red;font-size:20px;font-weight:bold;font-family:宋体;}")

        self.label3_1 = QLabel(self)
        self.label3_1.setFixedWidth(100)#右边的长度(加上人数宽度了的)
        self.label3_1.move(350, 60)
        self.label3_1.setStyleSheet("QLabel{color: red;font-size:20px;font-weight:bold;font-family:宋体;}")
        #
        self.label4 = QLabel(self)
        self.label4.setFixedWidth(350)
        self.label4.move(20, 80)
        self.label4.setStyleSheet("QLabel{color: red;font-size:20px;font-weight:bold;font-family:宋体;}")

        self.label4_1 = QLabel(self)
        self.label4_1.setFixedWidth(100)#右边的长度(加上人数宽度了的)
        self.label4_1.move(350, 80)
        self.label4_1.setStyleSheet("QLabel{color: red;font-size:20px;font-weight:bold;font-family:宋体;}")

        text2 = 'NA-PVP-Official-Aberration312'
        text1 = 'EU-PVP-Official-TheIsland170 '
        text3 = 'NA-PVP-Official-Valguero576  '
        text4 = 'NA-PVP-Official-GenOne618    '
        Server = '加载中....'
        # self.label.setText(text)
        self.label1.setText(text1)
        self.label2.setText(text2)
        self.label3.setText(text3)
        self.label4.setText(text4)

        self.label1_1.setText(Server)
        self.label2_1.setText(Server)
        self.label3_1.setText(Server)
        self.label4_1.setText(Server)

        time.sleep(8)

        self.Mytimer()

    def Mytimer(self):
        timer = QTimer(self)
        timer.timeout.connect(self.update)
        timer.start(100)
    #
    def update(self):
        self.label.setText(T_test)
        self.label1_1.setText(Server_170)
        self.label2_1.setText(Server_312)
        self.label3_1.setText(Server_576)
        self.label4_1.setText(Server_618)


def mainwindows():
    app = QtWidgets.QApplication(sys.argv)
    my = showTime()
    my.show()
    sys.exit(app.exec_())


if __name__ == '__main__':

    workThread = WorkThread()
    workThread.start()

    workThread1 = WorkThread1()
    workThread1.start()

    print('开始运行')
    mainwindows()
